//
//  DXWTabBarController.m
//  BuDeJie
//
//  Created by 淡馨慰 on 16/7/25.
//  Copyright © 2016年 淡馨慰. All rights reserved.
//

#import "DXWTabBarController.h"
#import "DXWEssenceViewController.h"
#import "DXWNewViewController.h"
#import "DXWPublishViewController.h"
#import "DXWFriendTrendViewController.h"
#import "DXWMeViewController.h"
#import "DXWNavigationController.h"

#define tabBarW  self.tabBar.frame.size.width
#define tabBarH  self.tabBar.frame.size.height

@interface DXWTabBarController ()

/** 发布按钮 */
@property (nonatomic, weak) UIButton *publishBtn;

@end


@implementation DXWTabBarController

-(UIButton *)publishBtn
{
    if (_publishBtn == nil) {
        NSLog(@"创建了发布按钮");
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _publishBtn = btn;
        /**
         *  自定义按钮设置图片一定要有图片的状态!!!!
         */
        [btn setImage:[UIImage imageRender:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageRender:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        [btn addTarget:self action:@selector(publishBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        /**
         *  设置按钮尺寸一定要在设置位置前!!
         */

        [btn sizeToFit];
//       _publishBtn.center = CGPointMake(tabBarW * 0.5, tabBarH * 0.5);
        [self.tabBar addSubview:btn];
    }
    return _publishBtn;
    
}
/**
 *  监听发布按钮的点击
 */
- (void)publishBtnClick
{
    NSLog(@"点击发布按钮");
}

/**
 *  方法1:设置tabBar的标题
 */
+ (void)initialize
{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSLog(@"进行了DXWTabBarController的一次性操作");
        
        
        /* -----------关于appearance-----------------
        1.任何对象都能使用appearance吗?
            只要`遵守了UIAppearance协议`就可以使用appearance
            同理:UIView遵守了UIAppearance协议,其子类也可以用
        2.任何属性都可以使用appearance设置吗?
            不能.eg:设置title就不行
        3.哪些属性可以通过appearance设置?
            只有带有UI_APPEARANCE宏属性的,才能去设置`
        4.如果使用appearance设置属性,必须要在显示之前去设置 -- 若显示之后再设置就晚了
         -------------------------------------------*/
        
        UITabBarItem *item = [UITabBarItem appearance];
        NSDictionary *dictOfNormal = @{
                                       NSFontAttributeName:[UIFont systemFontOfSize:14] 
                             
                                       };
        [item setTitleTextAttributes:dictOfNormal forState:UIControlStateNormal];
        //注意:不要写成@"NSForegroundColorAttributeName"
        NSDictionary *dictOfSelected = @{
                                         NSForegroundColorAttributeName :[UIColor blackColor],
                                         
                                         };
        [item setTitleTextAttributes:dictOfSelected forState:UIControlStateSelected ];
    });
}
/**
 *  方法2:设置tabBar的标题
 */
//+ (void)load
//{
//    UITabBarItem *item = [UITabBarItem appearance];
//    NSDictionary *dictOfNormal = @{
//                           NSFontAttributeName:[UIFont systemFontOfSize:20]
//                           };
//    [item setTitleTextAttributes:dictOfNormal forState:UIControlStateNormal];

////注意:不要写成@"NSForegroundColorAttributeName"
//    NSDictionary *dictOfSelected = @{
//                    NSForegroundColorAttributeName :[UIColor blackColor],
//
//                                     };
//    [item setTitleTextAttributes:dictOfSelected forState:UIControlStateSelected ];
//}

- (void)viewDidLoad {
    [super viewDidLoad];

    //1. 添加子控制器
    [self setUpChildVc];
    
    //2. 设置tabBar内容
    [self setUpTabBar];
    
    //怎么会是setter方法??
    self.publishBtn.center = CGPointMake(tabBarW * 0.5, tabBarH * 0.5);

}


#pragma mark -设置tabBar内容
- (void)setUpTabBar
{
    //获取所有tabBar按钮的标题
    
    
    //多态:父类指针指向子类对象
    // 0 -- 精华
    UIViewController *vc0 = self.childViewControllers[0];
    vc0.tabBarItem.title = @"精华";
    vc0.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
    vc0.tabBarItem.selectedImage = [UIImage imageRender:@"tabBar_essence_click_icon"];
    
    // 1 -- 新帖
    UIViewController *vc1 = self.childViewControllers[1];
    vc1.tabBarItem.title = @"新帖";
    vc1.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"];
    vc1.tabBarItem.selectedImage = [UIImage imageRender:@"tabBar_new_click_icon"];
    

    // 2 -- 发布 ==> 没有导航条
    UIViewController *vc2 = self.childViewControllers[2];
    //使用系统按钮占位,自订一个一个按钮
    vc2.tabBarItem.enabled = NO;
    //添加自定义的发布按钮
//    [vc2.tabBarController.tabBar addSubview:self.publishBtn];

    
    // 3 -- 关注
    UIViewController *vc3 = self.childViewControllers[3];
    vc3.tabBarItem.title = @"关注";
    vc3.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"];
    vc3.tabBarItem.selectedImage = [UIImage imageRender:@"tabBar_friendTrends_click_icon"];
    
    
    // 4 -- 我
    UIViewController *vc4 = self.childViewControllers[4];
    vc4.tabBarItem.title = @"我";
    vc4.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"];
    vc4.tabBarItem.selectedImage = [UIImage imageRender:@"tabBar_me_click_icon"];
    
    
}

#pragma mark -添加子控制器
- (void)setUpChildVc
{
    //设置tabBarVC的子控制器
    /**
     *  分析:除中间按钮点击跳转时无顶部导航条,其余都有,则创建4个导航控制器,导航控制器再添加对应子控制器
     */
    // 0 -- 精华
    DXWEssenceViewController *vc0 = [[DXWEssenceViewController alloc]init];
    vc0.view.backgroundColor = [UIColor grayColor];
    DXWNavigationController *nav0 = [[DXWNavigationController alloc]initWithRootViewController:vc0];
    [self addChildViewController:nav0];
    
    // 1 -- 新帖
    DXWNewViewController *vc1 = [[DXWNewViewController alloc]init];
    vc1.view.backgroundColor = [UIColor yellowColor];
    DXWNavigationController *nav1 = [[DXWNavigationController alloc]initWithRootViewController:vc1];
    [self addChildViewController:nav1];
    
    // 2 -- 发布 ==> 没有导航条
    DXWPublishViewController *vc2 = [[DXWPublishViewController alloc]init];
    vc2.view.backgroundColor = [UIColor orangeColor];
    [self addChildViewController:vc2];
    
    
    // 3 -- 关注
    DXWFriendTrendViewController *vc3 = [[DXWFriendTrendViewController alloc]init];
//    vc3.view.backgroundColor = [UIColor cyanColor];
    DXWNavigationController *nav3 = [[DXWNavigationController alloc]initWithRootViewController:vc3];
    [self addChildViewController:nav3];
    
    // 4 -- 我
    DXWMeViewController *vc4 = [[DXWMeViewController alloc]init];
    vc4.view.backgroundColor = [UIColor magentaColor];
    DXWNavigationController *nav4 = [[DXWNavigationController alloc]initWithRootViewController:vc4];
    [self addChildViewController:nav4];
}

@end
